package com.manage.project.biz.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizGonglu;
import com.manage.project.biz.domain.BizSuidao;
import com.manage.project.biz.domain.CivilEnginneeringEmergencyCheck;
import com.manage.project.biz.domain.CivilEnginneeringFacility;
import com.manage.project.biz.service.*;
import com.manage.project.system.domain.SysNotice;
import com.manage.project.system.service.ISysNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 应急检查Controller
 *
 * @author 小赵
 * @date 2022-04-28
 */
@RestController
@RequestMapping("/civil/emergencycheck")
public class CivilEnginneeringEmergencyCheckController extends BaseController
{
    @Autowired
    private ICivilEnginneeringEmergencyCheckService civilEnginneeringEmergencyCheckService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;
//    @Autowired
//    private ICivilEnginneeringSpecialInspectionService iCivilEnginneeringSpecialInspectionService;

    /**
     * 查询应急检查列表
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        startPage();
        List<CivilEnginneeringEmergencyCheck> list = civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckList(civilEnginneeringEmergencyCheck);
        return getDataTable(list);
    }

    /**
     * 导出应急检查列表
     */
    @Log(title = "应急检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck) throws IOException {
        List<CivilEnginneeringEmergencyCheck> list = civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckList(civilEnginneeringEmergencyCheck);
        ExcelUtil<CivilEnginneeringEmergencyCheck> util = new ExcelUtil<CivilEnginneeringEmergencyCheck>(CivilEnginneeringEmergencyCheck.class);
         return util.exportExcel( list, "应急检查数据");
    }
    /**
     * 获取应急检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:query')")
    @GetMapping(value = "/{emergencyCheckId}")
    public AjaxResult getInfo(@PathVariable("emergencyCheckId") Long emergencyCheckId)
    {
        return AjaxResult.success(civilEnginneeringEmergencyCheckService.selectCivilEnginneeringEmergencyCheckByEmergencyCheckId(emergencyCheckId));
    }

    /**
     *添加应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:add')")
    @Log(title = "添加应急检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        String roadId= civilEnginneeringEmergencyCheck.getRoadId();
        String roadName=civilEnginneeringEmergencyCheck.getRoadName();
        String tunnelName=civilEnginneeringEmergencyCheck.getTunnelName();
        String tunnelId=civilEnginneeringEmergencyCheck.getTunnelId();
        String facility=civilEnginneeringEmergencyCheck.getFacilityName();
        String mileageNumber=civilEnginneeringEmergencyCheck.getMileageNumber();
        Date time=civilEnginneeringEmergencyCheck.getInspectionDate();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
//        String strDate= strdate.format(time);
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }

        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }

        if(civilEnginneeringEmergencyCheck.getPositionNumber()!=0){
            SysNotice sysNotice= new SysNotice();
//            sysNotice.setNoticeTitle(tunnelName+"土建结构"+"“"+facility+"”"+"专项检查");
            sysNotice.setNoticeTitle("土建结构-专项检查");
            sysNotice.setNoticeType("1");
            sysNotice.setStatus("0");
            sysNotice.setNoticeContent("应急检查提交："+roadId+roadName+"-"+tunnelId+tunnelName+"-"+mileageNumber+facility+"检查任务");
            sysNotice.setCreateBy(getUsername());
            sysNotice.setCreateTime(time);
            noticeService.insertNotice(sysNotice);
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null){
            return new AjaxResult().error("当前公路不存在，请输入正确的道路信息");
        }
        else{
            return toAjax(civilEnginneeringEmergencyCheckService.insertCivilEnginneeringEmergencyCheck(civilEnginneeringEmergencyCheck));
        }
    }

    /**
     * 修改应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:edit')")
    @Log(title = "应急检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringEmergencyCheck civilEnginneeringEmergencyCheck)
    {
        String roadName=civilEnginneeringEmergencyCheck.getRoadName();
        String tunnelName=civilEnginneeringEmergencyCheck.getTunnelName();
        String tunnelId=civilEnginneeringEmergencyCheck.getTunnelId();
        String facility=civilEnginneeringEmergencyCheck.getFacilityName();
        String mileageNumber=civilEnginneeringEmergencyCheck.getMileageNumber();
        Date time=civilEnginneeringEmergencyCheck.getInspectionDate();
        SimpleDateFormat strdate = new SimpleDateFormat("yyyy-MM-dd");
        String strDate= strdate.format(time);
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }

        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null){
            return new AjaxResult().error("当前公路不存在，请输入正确的道路信息");
        }else {
            return toAjax(civilEnginneeringEmergencyCheckService.updateCivilEnginneeringEmergencyCheck(civilEnginneeringEmergencyCheck));
        }
        }

    /**
     * 删除应急检查
     */
    @PreAuthorize("@ss.hasPermi('civil:emergencycheck:remove')")
    @Log(title = "应急检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{emergencyCheckIds}")
    public AjaxResult remove(@PathVariable Long[] emergencyCheckIds)
    {
        return toAjax(civilEnginneeringEmergencyCheckService.deleteCivilEnginneeringEmergencyCheckByEmergencyCheckIds(emergencyCheckIds));
    }

}
